package service.impl;

import global.Constants;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URLDecoder;
import java.util.Properties;

import net.sf.json.JSONObject;
import service.SettingService;

/**
 * 系统设置Service实现类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class SettingServiceImpl implements SettingService {
	/**
	 * 获得系统设置信息
	 * @return JSONObject 系统设置信息
	 */
	public JSONObject getSetting() throws Exception{
		JSONObject rs = new JSONObject();
		// 输入流
		InputStream is = null;
		try {
			// 加载配置文件
			is = Constants.class.getClassLoader().getResourceAsStream("../setting.properties");
			Properties p = new Properties();
			p.load(is);
			rs.put("emailSmtpHost", p.getProperty("setting.emailSmtpHost"));
			rs.put("emailAccount", p.getProperty("setting.emailAccount"));
			rs.put("emailPassword", p.getProperty("setting.emailPassword"));
			rs.put("imageWidth", p.getProperty("setting.imageWidth"));
			rs.put("imageHeight", p.getProperty("setting.imageHeight"));

			rs.put("networkType", p.getProperty("setting.networkType"));
			rs.put("proxyIp", p.getProperty("setting.proxyIp"));
			rs.put("proxyPort", p.getProperty("setting.proxyPort"));
			rs.put("proxyUser", p.getProperty("setting.proxyUser"));
			rs.put("proxyPassword", p.getProperty("setting.proxyPassword"));
			if("true".equals(p.getProperty("setting.openRegister"))){
				rs.put("openRegister", "on");
			}else{
				rs.put("openRegister", "off");
			}
			return rs;
		} finally {
			if(null != is){
				is.close();
			}
		}
	}
	
	
	/**
	 * 保存系统设置信息
	 * @param JSONObject 系统设置信息
	 */
	public void saveSetting(JSONObject setting) throws Exception{
		// 创建输出流
		FileOutputStream fos = null;
		PrintStream ps = null;
		try {
			// 写入系统设置信息
			String path = URLDecoder.decode(Constants.class.getClassLoader().getResource("../setting.properties").getPath(),"utf-8");
			fos = new FileOutputStream(new File(path));
			ps = new PrintStream(fos);
			ps.println("setting.emailSmtpHost="+setting.optString("emailSmtpHost"));
			ps.println("setting.emailAccount="+setting.optString("emailAccount"));
			ps.println("setting.emailPassword="+setting.optString("emailPassword"));
			ps.println("setting.imageWidth="+setting.optString("imageWidth"));
			ps.println("setting.imageHeight="+setting.optString("imageHeight"));
			if("on".equals(setting.optString("openRegister"))){
			    ps.println("setting.openRegister=true");
			}else{
			    ps.println("setting.openRegister=false");
			}

			// 代理信息
			ps.println("setting.networkType="+setting.optString("networkType"));
		    ps.println("setting.proxyIp="+setting.optString("proxyIp"));
		    ps.println("setting.proxyPort="+setting.optString("proxyPort"));
		    ps.println("setting.proxyUser="+setting.optString("proxyUser"));
		    ps.println("setting.proxyPassword="+setting.optString("proxyPassword"));
		} finally{
			if(null != ps){
				ps.close();
			}
			if(null != fos){
				fos.close();
			}
		}
	}
}
